<template>
  <u-modal :show="show" title="修改密码" showCancelButton @confirm="confirm" @cancel="cancel">
    <view class="slot-content">
      <u--form :model="pwd" ref="uForm" :rules="rules" labelWidth="160rpx" labelAlign="left">
        <u-form-item label="原密码" prop="oldPassword" class="u-border-bottom">
          <u--input v-model="pwd.oldPassword" type="password" placeholder="请输入原密码" border="none"></u--input>
        </u-form-item>
        <u-form-item label="新密码" prop="newPassword" class="u-border-bottom">
          <u--input v-model="pwd.newPassword" type="password" placeholder="请输入新密码" border="none"></u--input>
        </u-form-item>
        <u-form-item label="确认密码" prop="confirmPwd" class="u-border-bottom">
          <u--input v-model="pwd.confirmPwd" type="password" placeholder="请输入确认密码" border="none"></u--input>
        </u-form-item>
      </u--form>
    </view>
  </u-modal>
</template>

<script>
import * as UserApi from '@/api/system/user'

export default {
  props: {
    show: Boolean
  },
  data () {
    return {
      pwd: {
        oldPassword: '',
        newPassword: '',
		confirmPwd: ''
      },
	  rules: {
	    oldPassword: [ { required: true, message: '原密码不能为空', trigger: ['blur', 'change'] } ],
	    newPassword: [ { required: true, message: '新密码不能为空', trigger: ['blur', 'change'] } ],
		confirmPwd: [
			{ required: true, message: "确认密码不能为空", trigger: "blur" },
			{
				// 自定义验证函数，见上说明
				validator: (rule, value, callback) => {
					if(this.pwd.newPassword !== value){
						return false;
					}
					return true
				},
				message: '两次输入的密码不一致',
				// 触发器可以同时用blur和change
				trigger: ['change','blur'],
			}
		]
	  }
    }
  },
  methods: {
    confirm () {
      this.$refs.uForm.validate().then(res => {
		//console.log(this.pwd)
        UserApi.userPasswordModify(this.pwd).then(res => {
         uni.showToast({ title: res.msg })
		  this.cancel()
        })
      });
    },
    cancel () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
